package com.yaowk.system.server.entity;

import com.yaowk.common.db.BaseEntity;
import com.yaowk.common.db.GeneratorNote;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Getter
@Setter
@ToString
@Entity
@DynamicInsert
@DynamicUpdate
public class SysMenu extends BaseEntity<Long> {

    @GeneratorNote("菜单名称")
    private String name;
    @GeneratorNote("菜单描述")
    private String description;
    @GeneratorNote("对应路径")
    private String url;
    @GeneratorNote("上级id")
    @Column(insertable = false, updatable = false)
    private Long parentId;
    @GeneratorNote("排序，数字越大越靠前")
    @ColumnDefault("0")
    @Column(nullable = false)
    private Byte sort;
    @GeneratorNote("状态")
    @ColumnDefault("1")
    @Column(nullable = false)
    private Byte status;
    @GeneratorNote("类型 1-下拉2-按钮3-隐藏")
    @ColumnDefault("1")
    @Column(nullable = false)
    private Byte type;
    @GeneratorNote("创建时间")
    @Column(nullable = false, updatable = false)
    @CreationTimestamp
    private Date createTime;

    @OneToMany(mappedBy = "parent")
    private Set<SysMenu> children;

    @ManyToOne
    @JoinColumn(name = "parentId")
    private SysMenu parent;

    @ManyToMany(mappedBy = "menus")
    private Set<SysRole> roles;

    @ManyToMany
    @JoinTable(
            name = "sys_menu_resource",
            joinColumns = @JoinColumn(name = "menu_id"),
            inverseJoinColumns = @JoinColumn(name = "resource_id")
    )
    private Set<SysResource> resources;
}
